<?php
/**
 * Created by PhpStorm.
 * User: M2
 * Date: 13-11-11
 * Time: 下午6:25
 */
class YlCommentsTaglib {
    public function __construct() {
        C(include SITE_PATH.'/extends/taglib/tagarr.php');
    }
    public function parse($content,$contentId) {
        if (stripos($content, 'yl:comments') !== false) {
            $matches = preg_parse($content, 'yl:comments');
            foreach ($matches as $match) {
                $parseStr = '';
                $commentsText = getParseContentText($match, 'yl:comments');
                if(isset($contentId) && !empty($contentId)) {
                    $list = M('cms_comments')->where("article_id=$contentId")->select();
                    if(!empty($list)) {
                        foreach($list as $k => $v) {
                            $text = taglib(C('yl:comment'))->parse($commentsText,$v);
                            if(strpos($text,'{') && strpos($text,'}')) {
                                preg_match_all('/\{Comment.(.*?)\}/eis',$text,$out);
                                for ($i=0; $i< count($out[1]); $i++) {
                                    $text = str_ireplace($out[0][$i],$v[$out[1][$i]],$text);
                                }
                            }
                            $parseStr .= $text;
                        }
                    } else {
                        $parseStr = '';
                    }
                }
                $content = str_replace($match, $parseStr, $content);
            }
        }
        return $content;
    }
}